package com.study.chapter06;

import com.study.entity.Event;
import com.study.entity.UrlViewCount;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.SlidingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

import java.time.Duration;

/**
 * @Description: 延时数据处理
 * @Author: LiuQun
 * @Date: 2022/8/4 21:55
 */
public class LateDataProcessTest {
    public static void main(String[] args) throws Exception {
        //环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        SingleOutputStreamOperator<Event> dataOpt = env.socketTextStream("192.168.30.100", 7777)
                .map(new MapFunction<String, Event>() {

                    @Override
                    public Event map(String line) throws Exception {
                        String[] dataArr = line.split(",");
                        return new Event(dataArr[0].trim(), dataArr[1].trim(), Long.valueOf(dataArr[2].trim()));
                    }
                });

        //水位线
        SingleOutputStreamOperator<Event> watermarkOpt = dataOpt.assignTimestampsAndWatermarks(
                WatermarkStrategy
                        //方式一：设置watermark延时3s
                        .<Event>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                        .withTimestampAssigner((data, l) -> data.timestamp)
        );
        //定义侧输出流标签
        OutputTag<Event> outputTag = new OutputTag<>("late");

        //设置窗口
        SingleOutputStreamOperator<UrlViewCount> result = watermarkOpt.keyBy(data -> data.url) //根据url分组
                .window(SlidingEventTimeWindows.of(Time.seconds(10), Time.seconds(5)))
                // 方式二：允许窗口处理迟到数据，设置1分钟的等待时间
                .allowedLateness(Time.minutes(1))
                // 方式三：将最后的迟到数据输出到侧输出流
                .sideOutputLateData(outputTag)
                .aggregate(
                        new UrlViewCountAgg(),
                        new UrlViewCountProcess()
                );

        result.print("result");
        //获取迟到的数据
        DataStream<Event> sideOutput = result.getSideOutput(outputTag);
        sideOutput.print("lateData");

        env.execute();
    }

    /**
     * 自定义AggregateFunction
     */
    public static class UrlViewCountAgg implements AggregateFunction<Event,Long,Long> {

        @Override
        public Long createAccumulator() {
            //初始数量为0
            return 0L;
        }

        @Override
        public Long add(Event value, Long accumulator) {
            return accumulator + 1;
        }

        @Override
        public Long getResult(Long accumulator) {
            return accumulator;
        }

        @Override
        public Long merge(Long a, Long b) {
            return null;
        }
    }


    /**
     * 自定义ProcessWindowFunction
     */
    public static class  UrlViewCountProcess extends ProcessWindowFunction<Long,UrlViewCount,String, TimeWindow>{

        @Override
        public void process(String url, Context context, Iterable<Long> elements, Collector<UrlViewCount> out) throws Exception {
            long start = context.window().getStart();
            long end = context.window().getEnd();
            // 迭代器中只有一个元素，就是增量聚合函数的计算结果
            Long num = elements.iterator().next();
            out.collect(new UrlViewCount(url, num, start, end));
        }
    }
}
